package com.xxg.renrenorder.dao;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Indexes;
import com.xxg.renrencommon.domain.bean.UserSpreadBenefitInfo;
import com.xxg.renrencommon.domain.filedenum.userenum.BenefitStatusEnum;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * 收益dao
 *
 * @author szj
 * @date 2021/12/20
 */
@Repository
public class UserSpreadBenefitInfoDao {
    private final MongoTemplate mongoTemplate;

    public UserSpreadBenefitInfoDao(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
        createCollection();
    }

    /**
     * 创建collection
     */
    private void createCollection() {
        String collectionName = "userSpreadBenefitInfo";
        MongoCollection<Document> collection;
        if (!mongoTemplate.getCollectionNames().contains(collectionName)) {
            collection = mongoTemplate.createCollection(UserSpreadBenefitInfo.class);
            createIndex(collection);
        }
    }

    /**
     * 创建索引
     *
     * @param collection 数据集
     */
    private void createIndex(MongoCollection<Document> collection) {
        collection.createIndex(Indexes.descending("orderId"));
        collection.createIndex(Indexes.descending("userId"));
        collection.createIndex(Indexes.descending("afterSalesOrderId"));
        collection.createIndex(Indexes.compoundIndex(Indexes.descending("userId"), Indexes.descending("orderId")));
        collection.createIndex(Indexes.compoundIndex(Indexes.descending("userId"), Indexes.descending("afterSalesOrderId")));
        collection.createIndex(Indexes.compoundIndex(Indexes.descending("userId"), Indexes.descending("benefitStatus")));
    }

    public void insert(UserSpreadBenefitInfo userSpreadBenefitInfo) {
        userSpreadBenefitInfo.setCreateTime(new Date());
        userSpreadBenefitInfo.setUpdateTime(new Date());
        mongoTemplate.insert(userSpreadBenefitInfo);
    }

    /**
     * 更新收益记录为已结算
     *
     * @param id 订单ID
     */
    public void updateBenefitStatus(String id) {
        Query query = new Query(where("orderId").is(id).and("benefitStatus").is(BenefitStatusEnum.UNSETTLED.getValue()));
        Update update = new Update();
        update.set("updateTime", new Date());
        update.set("benefitStatus", BenefitStatusEnum.SETTLED.getValue());
        mongoTemplate.updateMulti(query, update, UserSpreadBenefitInfo.class);
    }

    /**
     * 该售后订单的收益已经处理过
     *
     * @param orderId 订单ID
     * @param id      售后订单ID
     * @return 结果
     */
    public long hasRecords(String orderId, String id) {
        Query query = new Query(where("orderId").is(orderId).and("afterSalesOrderId").is(id));
        return mongoTemplate.count(query, UserSpreadBenefitInfo.class);
    }

    public Integer findBenefitStatusByOrderId(String orderId) {
        Query query = new Query(where("orderId").is(orderId));
        UserSpreadBenefitInfo one = mongoTemplate.findOne(query, UserSpreadBenefitInfo.class);
        if (one == null) {
            return BenefitStatusEnum.UNSETTLED.getValue();
        }
        return one.getBenefitStatus();
    }
}
